package com.horoscope.astrology.zodiac.signs.free.activity

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.horoscope.astrology.zodiac.signs.free.AppSettingManager
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.adapter.SubPagerAdapter
import com.horoscope.astrology.zodiac.signs.free.bean.DataResultBean
import com.horoscope.astrology.zodiac.signs.free.bean.UserBean
import com.horoscope.astrology.zodiac.signs.free.network.NetworkEngine
import com.horoscope.astrology.zodiac.signs.free.network.NetworkListener
import com.horoscope.astrology.zodiac.signs.free.utils.EventType
import com.horoscope.astrology.zodiac.signs.free.utils.FlurryEventUtil
import com.horoscope.astrology.zodiac.signs.free.utils.SysUtils
import kotlinx.android.synthetic.main.activity_horoscope.*
import kotlinx.android.synthetic.main.view_network_error.*

/**
 * 星座运势
 * */
class HoroscopeActivity : BaseActivity() {

    private lateinit var mViewPager: ViewPager
    private lateinit var mTabLayout: TabLayout
    private var mBean = UserBean()
    private var tabSize = 4

    companion object {
        private val TAG = HoroscopeActivity::class.java.simpleName
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_horoscope)
        initTitleBar(
            this@HoroscopeActivity,
            R.id.btn_back_edit_name,
            R.id.text_view_back_title,
            getString(R.string.app_name_2)
        )

        initView()
    }

    private fun initView() {
        mBean = AppSettingManager.readObject("user_info") ?: return

        mTabLayout = findViewById(R.id.tab_layout_main)
        mViewPager = findViewById(R.id.view_pager_main)
        mViewPager.offscreenPageLimit = 3
        mViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                when (position) {
                    0 -> {
                        // 星座的哪一部分展示出来
                        FlurryEventUtil.putDataToEvent(
                            EventType.TYPE_HOROSCOPE_CONTENT_SHOW,
                            "type",
                            "Today"
                        )
                    }
                    1 -> {
                        FlurryEventUtil.putDataToEvent(
                            EventType.TYPE_HOROSCOPE_CONTENT_SHOW,
                            "type",
                            "Tomorrow"
                        )
                    }
                    2 -> {
                        FlurryEventUtil.putDataToEvent(
                            EventType.TYPE_HOROSCOPE_CONTENT_SHOW,
                            "type",
                            "Month"
                        )
                    }
                    3 -> {
                        FlurryEventUtil.putDataToEvent(
                            EventType.TYPE_HOROSCOPE_CONTENT_SHOW,
                            "type",
                            "Year"
                        )
                    }
                }
            }

        })
        mTabLayout.post {
            setTabListener()
        }

        loadData()

        tv_network_ok.setOnClickListener {
            include_error.visibility = View.GONE
            loadData()
        }
    }

    private fun loadData() {
        showLoading()
        NetworkEngine.getMainData(mBean.constellationName, listener)
    }

    private val listener = object : NetworkListener<DataResultBean>() {
        override fun onSuccess(t: DataResultBean) {
            if (t.data.newyear2020.isEmpty()) {
                mViewPager.adapter = SubPagerAdapter(supportFragmentManager, 4, t)
                mTabLayout.setupWithViewPager(mViewPager)
                setTabView()
            }
            showLoaded()
        }

        override fun onFailed(t: DataResultBean) {
            super.onFailed(t)
            showLoaded()
            include_error.visibility = View.VISIBLE
        }

        override fun onError(e: String?) {
            super.onError(e)
            showLoaded()
            include_error.visibility = View.VISIBLE
        }

    }

//    private fun saveData(t: DataResultBean) {
//        AppSettingManager.saveList(t.data.today, "Today")
//        AppSettingManager.saveList(t.data.tomorrow, "Tomorrow")
//        AppSettingManager.saveString(t.data.month, "Month")
//        AppSettingManager.saveString(t.data.year, "Year")
//        AppSettingManager.saveString(t.data.newyear2020, SysUtils.getNexYear())
//    }

    private fun setTabView() {
        mTabLayout.getTabAt(0)!!.customView =
            getTabView(resources.getString(R.string.sub_fragment_today))
        mTabLayout.getTabAt(1)!!.customView =
            getTabView(resources.getString(R.string.sub_fragment_tomorrow))
        mTabLayout.getTabAt(2)!!.customView =
            getTabView(resources.getString(R.string.sub_fragment_month))
        mTabLayout.getTabAt(3)!!.customView =
            getTabView(resources.getString(R.string.sub_fragment_year))
    }

    // 自定义每个Tab
    private fun getTabView(title: String): View {
        val v = LayoutInflater.from(this@HoroscopeActivity).inflate(R.layout.tab_item_view, null)
        val textView = v!!.findViewById<TextView>(R.id.text_view_tab)
        textView.text = title
        val new = v.findViewById<TextView>(R.id.text_view_new)
        if (title.contains(SysUtils.getNexYear())) {
            if (!AppSettingManager.readBoolean("check_2020", false)) {
                new.visibility = View.VISIBLE
            } else {
                new.visibility = View.INVISIBLE
                textView.text = SysUtils.getNexYear()
            }
        } else {
            new.visibility = View.INVISIBLE
        }
        return v
    }


    private fun setTabListener() {
        for (i in 0 until mTabLayout.tabCount) {
            val tab = mTabLayout.getTabAt(i)
            if (tab != null) {
                if (tab.customView != null) {
                    val tabView = tab.customView!!.parent as View
                    tabView.tag = i
                    tabView.setOnClickListener(mTabListener)
                }
            }
        }
    }

    private val mTabListener = View.OnClickListener { v ->
        when (v?.tag as? Int) {
            0 -> {
                if (tabSize == 5) {
                    dismissNew()
                }

            }
        }
    }

    // 隐藏掉New布局，并保存到sp
    private fun dismissNew() {
        if (tabSize != 5) return
        val view = mTabLayout.getTabAt(0)!!.customView
        val new = view!!.findViewById<TextView>(R.id.text_view_new)
        new.visibility = View.INVISIBLE
        val title = view.findViewById<TextView>(R.id.text_view_tab)
        title.text = SysUtils.getNexYear()
        AppSettingManager.saveBoolean(true, "check_2020")

    }
}


